import Api from '../../utils/api.js';

const app = getApp();

// pages/myOrder/myOrder.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    appointmentList: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getMyOrders();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow (ops) {
    if (app.globalData.refresh_myOrder) {
      this.getMyOrders();
      app.globalData.refresh_myOrder = false;
    }
  },

  /* --==================数据接口==================-- */
  // 查询我的预约
  async getMyOrders () {
    const res = await Api.getMyApplications({ sessionKey: app.globalData.sessionKey });
    if (res) {
      const arr = res.message.map(item => {

        /* 1: 餐饮 2: 住宿 3: 会议 4: 车辆 5: 物业 */
        switch (item.application_type) {
        case '1':
          item = this.buildData(item,
            ['restaurant', '餐饮', '用餐人数', item.mealNum, '人', item.addTime, item.user_name]
          ); break;
        case '2':
          item = this.buildData(item,
            ['stay', '住宿', '入住人数', item.checkInNum, '人', item.addTime, item.user_name]); break;
        case '3':
          item = this.buildData(item,
            ['meeting', '会议', '会议室', item.meetingRoom, '', item.addTime, item.user_name]); break;
        case '4':
          item = this.buildData(item,
            ['car', '车辆', '车辆', item.carType, '', item.addTime, item.user_name]); break;
        case '5':
          item = this.buildData(item,
            ['property', '物业', '物业服务', item.commercialType, '', item.addTime, item.user_name]); break;
        default: break;
        }

        switch (item.current_step) {
        case '1': item.state = '待审核'; break;
        case '2': item.state = '审批中'; break;
        case '3': item.state = '已同意'; break;
        case '4': item.state = '已拒绝'; break;
        default: break;
        }
        return item;
      });
      app.globalData.myOrders = res.message;
      this.setData({ appointmentList: arr || [] });
    }
  },

  /* --==================功能函数==================-- */
  // 构建显示数据
  buildData (item, [typeText, type, typeInfo, value, unit, createTime, creator] = []) {
    return Object.assign({}, item, {
      typeText, type, typeInfo, value, unit, createTime, creator
    });
  },

  /* --==================事件处理==================-- */
  // 预约审核申请卡片点击
  handleOrderTap (e) {
    app.globalData.orderDetail = this.data.appointmentList.filter(order => order.application_id === +e.currentTarget.dataset.id)[0];
    wx.navigateTo({
      url: '/pages/orderDetail/orderDetail?oprType=detail'
    });
  }
});